package com.umakr.ax.app.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.umakr.ax.common.mapper.SpreadMapper;
import com.umakr.ax.common.model.RadioStation;
import com.umakr.ax.common.model.Spread;
import com.umakr.ax.common.model.SpreadContent;
import com.umakr.ax.common.model.SpreadRes;
import com.umakr.ax.common.service.SpreadService;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class SpreadWebService extends BaseService<SpreadMapper, Spread> {
    @Autowired
    SpreadService spreadService;

    @Autowired
    Environment env;

    public Object delSpread(Long id, Integer spreadStatus) {
        return spreadService.delSpread(id,spreadStatus);
    }

    public Object listSpreadByType(Long rsId, Integer spreadType) {
        Page<Map<String,Object>> page = baseMapper.listSpreadByType(rsId,spreadType);
        return page;
    }

    public Object listSpreadForShake(Long rsId, Integer spreadType,String searchText) {
        if("".equals(searchText)) {
            searchText=null;
        }

        Page<Map<String,Object>> page = baseMapper.listSpreadForShake(rsId,spreadType,searchText);

        for(Map<String,Object> map : page.getRecords()){
            Object status = map.get("spreadStatus");
            if(spreadType == 0){
                if((Integer.parseInt(status.toString())) == 0){
                    map.put("spreadStatus","审核中");
                }
            }
            if((Integer.parseInt(status.toString())) == 1){
                map.put("spreadStatus","已生效");
            }else if((Integer.parseInt(status.toString())) == 2){
                map.put("spreadStatus","已过期");
            }
        }

        return page;
    }


    public Object saveOrUpdateSpread(Spread spread,String spreadUrl,Long resultContentId){
        return spreadService.saveOrUpdateSpread(spread,spreadUrl,resultContentId);
    }

    public Object getSpreadInfoById(Long spreadId){
        String aliUrl = env.getProperty("oss.img.path");
        Map<String,Object>  map = baseMapper.getSpreadInfoById(spreadId);
        Object resPathObject = map.get("resPath");
        if(resPathObject != null){
            String resPath = map.get("resPath").toString();

            map.put("resPath", FormatPhotoUrlUtil.formatString(aliUrl,resPath));
            map.put("fileName",resPath.substring(resPath.lastIndexOf("/")+1));
        }
        return resultMap(map);
    }

}
